package streamforce;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Stat {
	private static final Log log = LogFactory.getLog(Stat.class); 
	private String name; 
	long count; 
	static long start, end; 
	public static Map<String, ArrayList<Stat>> stats = new HashMap<String, ArrayList<Stat>>(); 
		
	public Stat(String n){
		this.name = n;
		this.count = 0; 
		if (!stats.containsKey(this.name))
			stats.put(this.name, new ArrayList<Stat>()); 
		stats.get(this.name).add(this);		
	}
	
	public static void startTimer(){
		start = System.currentTimeMillis(); 
	}
	
	public static void endTimer(){
		end = System.currentTimeMillis() ;
	}
	
	public static long getElapsedTime(){
		return end-start; 
	}
	
	public void update(){
		this.count++;
	}
	
	public void reset(){
		this.count = 0 ;
	}
	
	public static void aggregateAndResetAll(String name){ 
		if (stats==null || stats.size()==0)
			return;
		
		int s = 0; 
		if (stats.get(name)!=null)
		for (Stat stat: stats.get(name)){ 
			s+=stat.getCount();
			stat.reset(); 
		}		
		log.info(name+": "+s); 		 
	}
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getCount() {
		return count;
	}
	public void setCount(long count) {
		this.count = count;
	}
		
}
